<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户账号" prop="accountId">
        <el-input
          v-model="queryParams.accountId"
          placeholder="请输入用户账号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择用户性别" clearable>
          <el-option
            v-for="dict in user_gender"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="帐号状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择帐号状态" clearable>
          <el-option
            v-for="dict in useraccount_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['userinfo:userinfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="userId" type="index" width="50"/>
      <el-table-column label="昵称" align="center" prop="nickName" width="70"/>
      <el-table-column label="账号" align="center" prop="accountId" width="50"/>
      <el-table-column label="邮箱" align="center" prop="email" width="100"/>
      <el-table-column label="手机号码" align="center" prop="phonenumber" width="120"/>
      <el-table-column label="性别" align="center" prop="sex" width="80px">
        <template #default="scope">
          <dict-tag :options="user_gender" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="帐号状态" align="center" prop="status" width="80px">
        <template #default="scope">
          <dict-tag :options="useraccount_type" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="155">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}-{i}-{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center" prop="remark" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="getUserAccount(scope.row)"
            v-hasPermi="['system:account:query']"
            >查看详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改信息管理信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="userinfoRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="部门ID" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门ID" />
        </el-form-item>
        <el-form-item label="用户账号" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户账号" />
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item label="用户账号id" prop="accountId">
          <el-input v-model="form.accountId" placeholder="请输入用户账号id" />
        </el-form-item>
        <el-form-item label="用户邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入用户邮箱" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="用户性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择用户性别">
            <el-option
              v-for="dict in user_gender"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-divider content-position="center">用户账户信息信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddAccount">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteAccount">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="accountList" :row-class-name="rowAccountIndex" @selection-change="handleAccountSelectionChange" ref="account">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="用户的登录名" prop="username" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.username" placeholder="请输入用户的登录名" />
            </template>
          </el-table-column>
          <el-table-column label="用户密码" prop="password" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.password" placeholder="请输入用户密码" />
            </template>
          </el-table-column>
          <el-table-column label="账户余额，精确到小数点后两位" prop="balance" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.balance" placeholder="请输入账户余额，精确到小数点后两位" />
            </template>
          </el-table-column>
          <el-table-column label="账户状态码，0-未激活，1-正常，2-冻结" prop="status" width="150">
            <template #default="scope">
              <el-select v-model="scope.row.status" placeholder="请选择账户状态码，0-未激活，1-正常，2-冻结">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="银行卡号" prop="cardNumber" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.cardNumber" placeholder="请输入银行卡号" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>


     <!-- 用户账户详情 -->
    <el-dialog title="用户账户详情" v-model="openAc" width="600px" @close="handleClose">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="登录名">{{
          acList.username
        }}</el-descriptions-item>
        <el-descriptions-item label="用户状态">
          <span v-if="acList.status === 0">停用</span>
          <span v-else>正常</span>
        </el-descriptions-item>
        <el-descriptions-item label="银行卡号">{{
          acList.cardNumber
        }}</el-descriptions-item>
        <el-descriptions-item label="账户金额">{{
          acList.balance
        }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup name="Userinfo">
import { listUserinfo, getUserinfo, delUserinfo, addUserinfo, updateUserinfo } from "@/api/userinfo/userinfo";

const { proxy } = getCurrentInstance();
const { useraccount_type, user_gender } = proxy.useDict('useraccount_type', 'user_gender');
import { getAccount } from "@/api/account/account.js";
const userinfoList = ref([]);
const accountList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedAccount = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    nickName: null,
    accountId: null,
    sex: null,
    status: null,
  },
  rules: {
    deptId: [
      { required: true, message: "部门ID不能为空", trigger: "blur" }
    ],
    userName: [
      { required: true, message: "用户账号不能为空", trigger: "blur" }
    ],
    nickName: [
      { required: true, message: "用户昵称不能为空", trigger: "blur" }
    ],
    accountId: [
      { required: true, message: "用户账号id不能为空", trigger: "blur" }
    ],
    userType: [
      { required: true, message: "用户类型不能为空", trigger: "change" }
    ],
    email: [
      { required: true, message: "用户邮箱不能为空", trigger: "blur" }
    ],
    phonenumber: [
      { required: true, message: "手机号码不能为空", trigger: "blur" }
    ],
    sex: [
      { required: true, message: "用户性别不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询信息管理信息列表 */
function getList() {
  loading.value = true;
  listUserinfo(queryParams.value).then(response => {
    userinfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  acList.value= ""
  reset();
}

// 表单重置
function reset() {
  form.value = {
    userId: null,
    deptId: null,
    userName: null,
    nickName: null,
    accountId: null,
    userType: null,
    email: null,
    phonenumber: null,
    sex: null,
    avatar: null,
    password: null,
    status: null,
    delFlag: null,
    loginIp: null,
    loginDate: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  accountList.value = [];
  proxy.resetForm("userinfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.userId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加信息管理信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _userId = row.userId || ids.value
  getUserinfo(_userId).then(response => {
    form.value = response.data;
    accountList.value = response.data.accountList;
    open.value = true;
    title.value = "修改信息管理信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["userinfoRef"].validate(valid => {
    if (valid) {
      form.value.accountList = accountList.value;
      if (form.value.userId != null) {
        updateUserinfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addUserinfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _userIds = row.userId || ids.value;
  proxy.$modal.confirm('是否确认删除信息管理信息编号为"' + _userIds + '"的数据项？').then(function() {
    return delUserinfo(_userIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 用户账户信息序号 */
function rowAccountIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 用户账户信息添加按钮操作 */
function handleAddAccount() {
  let obj = {};
  obj.username = "";
  obj.password = "";
  obj.balance = "";
  obj.status = "";
  obj.cardNumber = "";
  accountList.value.push(obj);
}

/** 用户账户信息删除按钮操作 */
function handleDeleteAccount() {
  if (checkedAccount.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的用户账户信息数据");
  } else {
    const accounts = accountList.value;
    const checkedAccounts = checkedAccount.value;
    accountList.value = accounts.filter(function(item) {
      return checkedAccounts.indexOf(item.index) == -1
    });
  }
}

/** 复选框选中数据 */
function handleAccountSelectionChange(selection) {
  checkedAccount.value = selection.map(item => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('userinfo/userinfo/export', {
    ...queryParams.value
  }, `userinfo_${new Date().getTime()}.xlsx`)
}

/** 查询用户账户下详情信息 */
const acList = ref([]);
const openAc = ref(false);
function getUserAccount(row) {
  getAccount(row.accountId).then((response) => {
    acList.value = response.data;
    console.log(acList.value);
  });
  openAc.value = true;
}
function handleClose() {
      // 这里可以添加额外的逻辑，比如确认关闭等
    acList.value = [];
};
getList();
</script>
